#!/usr/bin/env bash

#===================HEADER==========================|
#Author: Julio Lira <jul10l1r4@ufrn.edu.br>
#Website: jul10l1r4.github.io
#Nick: @jul10l1r4
#Description: This is program for analyzing of net- 
# work, IDS, Sniffer attack etc.
#License: GNU GPLv3

echo -e """
Cor \t Descrição
\033[31m%\033[0m \t Caso haja alguma colisão ou perda de pacote, erro em geral
\033[32m%\033[0m \t Caso esteja com bom funcionamento
"""

# Functions
_make()
{
	value=""
	iterate=$((($1*((`tput cols`-29)/4))/100))
	if [ $iterate -gt $(((`tput cols`-29)/4)) ] 
	then
					iterate=$((((`tput cols`-29)/4)))
	fi
	for i in `seq $iterate`
	do
		value+="%"		
	done
	
	left=$((((`tput cols` / 2) - 29) - $iterate))
		
	for i in `seq $left`
	do
		value+="-"
	done
}
_error(){
	if [ -z "$1" ]
	then
		echo -e "\033[31mHouve algum erro\033[0m"
	else
		echo -e "$1"
	fi
	exit 1
}
_iwPack()
{
	# Resolutions of errors
	[ "up" == "$(cat /sys/class/net/$1/operstate)" ] || _error "\033[31mConecte a uma rede sem fio!\033[0m";

	echo "Intensidade ao enviar/receber pacotes do A.P"
	media=`cat /sys/class/net/$1/tx_queue_len`0;
	
	while [ 1 ]
	do
		# get values
		catTx=`cat /sys/class/net/$1/statistics/tx_packets 2>>errors.log`
		catRx=`cat /sys/class/net/$1/statistics/rx_packets 2>>errors.log`
		catCTx=`cat /sys/class/net/$1/statistics/tx_errors 2>>errors.log`
		catCRx=`cat /sys/class/net/$1/statistics/rx_errors 2>>errors.log`
		andc="\033[0m"
	
		# Errors network, break packages etc...

		if [ "$catCTx" == "0" ]
		then
						ctx="\033[0;32m"
		else
						ctx="\033[31m"
		fi

		if [ "$catCRx" == "0" ]
		then
						crx="\033[0;32m"
		else
						crx="\033[31m"
		fi

		# call the function and return value
		_make $(((($catTx * 80) / $media)))

		tx="TX:$ctx $value $andc[$catTx|$(($catTx * 80 / $media))%](Pacotes)"

		# call the functoin and return valaue
		_make $((($catRx * 80 / $media)))
		rx="TR:$crx $value $andc[$catRx|$(($catRx * 80 / $media))%](pacotes)"
		
		# output
		echo -ne "$tx.$rx\r"

		# delay for processor
		sleep 0.2
	done
}

# Menu
if [ -z "$1" ]
then
	_iwPack "$(ip route show default | awk '/default/ {print $5}' | sed -n 1p)"
else
	_iwPack "$1"
fi
